package com.bawei.module_main.ui

import android.Manifest
import android.content.DialogInterface
import android.text.TextUtils
import android.view.MenuItem
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_common.aop.LoginCheck
import com.bawei.library_common.route.ARouterActivityPath
import com.bawei.library_common.route.ARouterFragmentPath
import com.bawei.library_common.utils.DataStoreUtils
import com.bawei.library_common.utils.DialogUtills
import com.bawei.library_common.utils.PermissionUtils
import com.bawei.library_mvi.ui.BaseActivity
import com.bawei.module_main.R
import com.bawei.module_main.adapter.MainViewPagerAdapter
import com.bawei.module_main.databinding.MainActivityMainBinding
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.navigation.NavigationBarView

@Route(path = ARouterActivityPath.PAGER_MAIN)
class MainActivity : BaseActivity<MainActivityMainBinding>() {

    override fun getLayout(): Int {
        return R.layout.main_activity_main
    }

    override fun initEnv() {
        super.initEnv()
        //viewpager
        val list = ArrayList<Fragment>()
        list.add(ARouter.getInstance().build(ARouterFragmentPath.PAGER_HOME).navigation() as Fragment)
        list.add(ARouter.getInstance().build(ARouterFragmentPath.PAGER_CINEMA).navigation() as Fragment)
        list.add(ARouter.getInstance().build(ARouterFragmentPath.PAGER_LIVE).navigation() as Fragment)
        list.add(ARouter.getInstance().build(ARouterFragmentPath.PAGER_MINE).navigation() as Fragment)
        val viewPagerAdapter = MainViewPagerAdapter(list,
            supportFragmentManager,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        viewDataBinding.vpMain.adapter = viewPagerAdapter

        //底部导航
        viewDataBinding.bnvMainBottombar.labelVisibilityMode = NavigationBarView.LABEL_VISIBILITY_LABELED
        val itemId = viewDataBinding.bnvMainBottombar.menu.getItem(3).itemId
        val badge = viewDataBinding.bnvMainBottombar.getOrCreateBadge(itemId)
        badge.badgeGravity = BadgeDrawable.BOTTOM_END //红点位置
        badge.isVisible = true
        badge.number = 120



    }

    override fun initEvent() {
        super.initEvent()
        requestPermissions()
        viewDataBinding.vpMain.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {

            }

            override fun onPageSelected(position: Int) {//0 1 2 3
                if(position >= 2){
                    viewDataBinding.bnvMainBottombar.menu.getItem(position+1).isChecked = true
                }
                viewDataBinding.bnvMainBottombar.menu.getItem(position).isChecked = true
                //判读用户是否登陆，后面统一处理
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        }
        )
        //点击底部
        viewDataBinding.bnvMainBottombar.setOnItemSelectedListener(object : NavigationBarView.OnItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when(item.itemId){
                    R.id.bar_home -> viewDataBinding.vpMain.currentItem = 0
                    R.id.bar_cinema -> viewDataBinding.vpMain.currentItem = 1
                    R.id.bar_send -> Toast.makeText(this@MainActivity,"发布攀段是否登陆",Toast.LENGTH_SHORT).show()
                    R.id.bar_msg -> viewDataBinding.vpMain.currentItem = 2
                    R.id.bar_mine -> mine()
                }
                return true
            }
        })
    }
    //我的页面：判断用户是否登陆过
    @LoginCheck
    private fun mine() {
       // viewDataBinding.vpMain.currentItem = 3
        if(TextUtils.isEmpty(DataStoreUtils.getData("token",""))){//没有登陆过
            ARouter.getInstance().build(ARouterActivityPath.PAGER_LOGIN).navigation()
        }else{//登陆过，执行后续操作
            viewDataBinding.vpMain.currentItem = 3
        }
    }

    fun requestPermissions(){
        PermissionUtils.getInstance().requestAppNeedPermission(this,object :PermissionUtils.PermissionCallback{
            override fun onSuccess() {
                //授予成功
                showMsg("权限授予成功")
            }

            override fun onFailed() {
//                //授予失败
                showConfirmDialog()
            }
        }, Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_PHONE_STATE)
    }

    private fun showConfirmDialog() {
        DialogUtills.showConfirmDialog(this,"权限授予失败","再次授予吗",
            "确定","取消",object :DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    requestPermissions()
                }
            },object :DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    showMsg("您选择不再授予")
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        DialogUtills.dismiss()
    }


}